<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>crossval.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>[itrn,itst]</span>=<span class=defun_name>crossval</span>(<span class=defun_in>num_data,num_folds</span>)<br>
<span class=h1>%&nbsp;CROSSVAL&nbsp;Partitions&nbsp;data&nbsp;for&nbsp;cross-validation.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;[itrn,itst]&nbsp;=&nbsp;crossval(num_data,num_folds)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;randomly&nbsp;partitions&nbsp;data&nbsp;into&nbsp;the&nbsp;training</span><br>
<span class=help>%&nbsp;&nbsp;and&nbsp;testing&nbsp;parts.&nbsp;The&nbsp;number&nbsp;of&nbsp;partitionings&nbsp;is&nbsp;determined</span><br>
<span class=help>%&nbsp;&nbsp;by&nbsp;the&nbsp;num_folds.&nbsp;If&nbsp;num_folds==1&nbsp;then&nbsp;makes&nbsp;only&nbsp;one&nbsp;random</span><br>
<span class=help>%&nbsp;&nbsp;partitioning&nbsp;of&nbsp;data&nbsp;into&nbsp;training&nbsp;and&nbsp;testing&nbsp;in&nbsp;ratio&nbsp;50:50.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;num_data&nbsp;[1x1]&nbsp;number&nbsp;of&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;num_folds&nbsp;[1x1]&nbsp;number&nbsp;of&nbsp;folders.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;itrn{i}&nbsp;Indices&nbsp;of&nbsp;training&nbsp;data&nbsp;of&nbsp;i-th&nbsp;folder.</span><br>
<span class=help>%&nbsp;&nbsp;itst{i}&nbsp;Indices&nbsp;of&nbsp;testing&nbsp;data&nbsp;i-th&nbsp;folder.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;19-aug-2004,&nbsp;VF,&nbsp;comments&nbsp;repared</span><br>
<span class=help1>%&nbsp;17-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;21-Jan-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;14-Jan-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;7-jan-2003,&nbsp;VF,&nbsp;created</span><br>
<br>
<hr>
<span class=comment>%&nbsp;random&nbsp;partitioning&nbsp;</span><br>
inx=randperm(num_data);<br>
<br>
itrn=cell(1,num_folds);<br>
itst=cell(1,num_folds);<br>
<br>
<span class=keyword>if</span>&nbsp;num_folds&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;half&nbsp;=&nbsp;fix(num_data/2);<br>
&nbsp;&nbsp;itrn{1}=inx(1:half);<br>
&nbsp;&nbsp;itst{1}=inx(half+1:<span class=keyword>end</span>);<br>
<span class=keyword>else</span><br>
<br>
&nbsp;&nbsp;num_column=ceil(num_data/num_folds);<br>
&nbsp;&nbsp;part=[1:num_data&nbsp;zeros(1,num_column*num_folds-num_data)];<br>
&nbsp;&nbsp;part=reshape(part,num_column,num_folds)';<br>
<br>
&nbsp;&nbsp;<span class=keyword>for</span>&nbsp;i=1:num_folds,<br>
&nbsp;&nbsp;&nbsp;&nbsp;tst_inx=part(i,:);<br>
&nbsp;&nbsp;&nbsp;&nbsp;tst_inx=tst_inx(find(tst_inx));<br>
&nbsp;&nbsp;&nbsp;&nbsp;trn_inx=part(find([1:num_folds]~=i),:);<br>
&nbsp;&nbsp;&nbsp;&nbsp;trn_inx=trn_inx(find(trn_inx));<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;itrn{i}=inx(trn_inx);<br>
&nbsp;&nbsp;&nbsp;&nbsp;itst{i}=inx(tst_inx);<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
